package com.reny.ll.git.base_logic

import android.app.Application
import com.alibaba.android.arouter.launcher.ARouter
import com.hjq.gson.factory.GsonFactory
import com.reny.ll.git.base_logic.app_task.InitARouter
import com.reny.ll.git.base_logic.app_task.InitCockroach
import com.reny.ll.git.base_logic.app_task.InitLogger
import com.reny.ll.git.base_logic.app_task.InitMMKV
import com.reny.ll.git.base_logic.di.httpClientModule
import com.reny.ll.git.base_logic.di.serviceModule
import com.reny.ll.git.base_logic.model.login.LoginInfo
import com.reny.ll.git.core.initBase
import com.reny.ll.git.core.taskdispatch.task.Task
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.MaterialHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import org.kodein.di.DI
import org.kodein.di.DIAware

object MApp {
    var loginInfo: LoginInfo? = null
    /*var userInfo: UserInfo? = null

    var oss: OSS? = null
    var DEVICE_TOKEN:String? = null*/
    @JvmField
    val gson = GsonFactory.getSingletonGson() //new GsonBuilder().setLenient().create();

}

interface DIFace : DIAware {

    override val di: DI
        get() = DI.lazy {
            mdi

            import(httpClientModule)
            import(serviceModule)
        }

    val mdi: DI.MainBuilder.() -> Unit
}


fun Application.init(vararg tasks: Task?) {
    initRefreshStyle()

    initBase(
        InitARouter(),
        InitMMKV(),
        InitLogger(),
        InitCockroach(),
        //InitWebview(),
        *tasks
    )
}

fun Application.onBaseTerminate() {
    //App.devDBHelper.close()
    ARouter.getInstance().destroy()
}

private fun initRefreshStyle() {
    //设置全局的Header构建器
    SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
        layout.setHeaderInsetStart(-8f)
        MaterialHeader(context).setColorSchemeColors(-0x1000000)
    }
    //设置全局的Footer构建器
    SmartRefreshLayout.setDefaultRefreshFooterCreator { context, _ -> ClassicsFooter(context) }
}